package com.dws.xchat.controller;

import java.util.Map;

import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dws.xchat.bean.User;
import com.dws.xchat.dao.UserDao;
import com.dws.xchat.exception.UserException;
import com.dws.xchat.util.ExceptionConstant;
import com.dws.xchat.util.UserConstant;
import com.mysql.jdbc.SQLError;

@Controller
@RequestMapping("regist")
public class RegistController {
	
	@Autowired
	private UserDao userDao;
	
	@RequestMapping
	public String index(){
		return "regist";
	}
	@RequestMapping(value="parameter",method=RequestMethod.POST)
	public String regsit(@RequestParam("username")String username,@RequestParam("password")String password,
			Map<String,Object> model){
		if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
			throw new UserException(ExceptionConstant.USERNAME_PASSWORD_EMPTY);
		}
		User user = new User(username,password);
		try{
			userDao.registUser(user);
		}catch (DuplicateKeyException e) {
			throw new UserException(ExceptionConstant.DUPLICATE_USERNAME);
		}
		model.put("info", UserConstant.REGIST_SUCCESS);
		return "index";
	}
}
